package com.mobdev.notepad;

import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;

public class PreferencesActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);

   	 	//is username has been set, show it in the summary
    	EditTextPreference usernamePreference = (EditTextPreference) getPreferenceScreen().findPreference("account_username");    	
    	if(usernamePreference != null)
    	{
    		//register listener for when username gets changed
    		usernamePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			
				@Override
				public boolean onPreferenceChange(Preference preference, Object newValue) {
					updateUsername(preference, newValue);
					
					return true;
				}
			}); 
    		
    		//show current username
    		String username = getPreferenceManager().getSharedPreferences().getString("account_username", null);
    		updateUsername(usernamePreference, username);
    	}
    }
  
    
    protected void updateUsername(Preference usernamePreference, Object username) 
    {   	
		if(usernamePreference != null && username != null)		
		{
			if( username.toString().trim().equals("") )
			{
				usernamePreference.setSummary( R.string.preference_account_username_summary );
			}
			else
			{
				//String summary = getResources().getString(R.string.preference_account_username_summary);	    		
				usernamePreference.setSummary("(" + username + ")");
			}
		}    	
    }
}